package com.fg.pro.trade.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.model.trade.dto.GmUnitDto;
import com.fg.pro.model.trade.dto.GmWarehouseDto;
import com.fg.pro.trade.dao.TradeWarehouseDao;
import com.fg.pro.trade.entity.TradeUnit;
import com.fg.pro.trade.entity.TradeWarehouse;
import com.fg.pro.trade.service.TradeWarehouseService;
import com.fg.pro.trade.utils.GmHttpUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.fg.pro.trade.utils.GmRequestUrl.*;

/**
 * 仓库(TradeWarehouse)表服务实现类
 *
 * @author makejava
 * @since 2025-08-15 10:06:11
 */
@Service("tradeWarehouseService")
public class TradeWarehouseServiceImpl extends ServiceImpl<TradeWarehouseDao, TradeWarehouse> implements TradeWarehouseService {

    @Autowired
    GmHttpUtils gmHttpUtils;
    @Autowired
    RedissonClient redissonClient;

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TradeWarehouse> list = query().list();
        PageInfo<TradeWarehouse> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean refresh(Date date) {
        RLock lock = redissonClient.getLock(REDIS_LOCK_URL+GM_WAREHOUSE_URL);
        boolean tryLock = lock.tryLock();
        if(!tryLock){
            return false;
        }
        try {
            return refreshTradeWarehouseByLock(date);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }
    @Transactional
    public boolean refreshTradeWarehouseByLock(Date date) throws InterruptedException {
        GmWarehouseDto.GmWarehouseReponse gmWarehouseReponse = gmHttpUtils.httpPostByToken(GM_WAREHOUSE_URL, new GmWarehouseDto().new GmWarehouseRequest().gmWarehouseRequest(999), GmWarehouseDto.GmWarehouseReponse.class);
        LinkedList<TradeWarehouse> newList = new LinkedList<>();
        for (GmWarehouseDto.GmWarehouseReponse.WarehouseData warehouseData : gmWarehouseReponse.getWarehouses()) {
            TradeWarehouse tradeWarehouse = new TradeWarehouse();
            tradeWarehouse.setId(warehouseData.getWarehouse_id());
            tradeWarehouse.setCreateTime(new Date(warehouseData.getCreate_time()));
            tradeWarehouse.setUpdateTime(new Date(warehouseData.getUpdate_time()));
            tradeWarehouse.setWarehouseGroupId(warehouseData.getGroup_id());
            tradeWarehouse.setWarehouseCustomizedCode(warehouseData.getCustomized_code());
            tradeWarehouse.setWarehouseName(warehouseData.getName());
            tradeWarehouse.setWarehouseAddress(warehouseData.getAddress());
            tradeWarehouse.setWarehouseGeoLatitude(warehouseData.getGeotag().getLatitude());
            tradeWarehouse.setWarehouseGeoLongitude(warehouseData.getGeotag().getLongitude());
            tradeWarehouse.setWarehouseContactName(warehouseData.getContact());
            tradeWarehouse.setWarehouseContactPhone(warehouseData.getPhone());
            tradeWarehouse.setWarehouseContactValid(warehouseData.getValid()?1:0);
            tradeWarehouse.setWarehouseDefault(ObjUtil.isEmpty(warehouseData.getIs_default())?0:(warehouseData.getIs_default()?1:0));
            newList.add(tradeWarehouse);
        }
        boolean save = saveOrUpdateBatch(newList);
        return save;
    }
}

